import pandas as pd
import seaborn as sns
import plotly.express as px
import matplotlib.pyplot as plt
import plotly.io as pio
pio.renderers.default = "plotly_mimetype+notebook"
For this excercise, we have written the following code to load the stock dataset built into plotly express.
stocks = px.data.stocks()
stocks.head()
| date | GOOG | AAPL | AMZN | FB | NFLX | MSFT | |
|---|---|---|---|---|---|---|---|
| 0 | 2018-01-01 | 1.000000 | 1.000000 | 1.000000 | 1.000000 | 1.000000 | 1.000000 |
| 1 | 2018-01-08 | 1.018172 | 1.011943 | 1.061881 | 0.959968 | 1.053526 | 1.015988 |
| 2 | 2018-01-15 | 1.032008 | 1.019771 | 1.053240 | 0.970243 | 1.049860 | 1.020524 |
| 3 | 2018-01-22 | 1.066783 | 0.980057 | 1.140676 | 1.016858 | 1.307681 | 1.066561 |
| 4 | 2018-01-29 | 1.008773 | 0.917143 | 1.163374 | 1.018357 | 1.273537 | 1.040708 |
Select a stock and create a suitable plot for it. Make sure the plot is readable with relevant information, such as date, values.
x = stocks["date"]
y = stocks["GOOG"]
fig, ax = plt.subplots(figsize = (20,8))
ax.plot(x,y, marker = 'o') #marker= rondje op ieder punt
ax.set_xlabel('date')
ax.set_ylabel('GOOG')
ax.set_title('Stocks information')
ax.set_xticks([0, 25, 50,75, 100])
plt.grid()
plt.show()
You've already plot data from one stock. It is possible to plot multiples of them to support comparison.
To highlight different lines, customise line styles, markers, colors and include a legend to the plot.
x = stocks["date"]
yGOOG = stocks["GOOG"]
yAAPL = stocks["AAPL"]
yAMZN = stocks["AMZN"]
yFB = stocks["FB"]
yNFLX = stocks["NFLX"]
yMSFT = stocks["MSFT"]
fig, ax = plt.subplots(figsize = (20,8))
ax.plot(x, yGOOG, marker = 'o')
ax.plot(x, yAAPL, marker = 'o')
ax.plot(x, yAMZN, marker = 'o')
ax.plot(x, yFB, marker = 'o')
ax.plot(x, yNFLX, marker = 'o')
ax.plot(x, yMSFT, marker = 'o')
#ax.legend('GOOG', 'AAPL', 'AMZN', 'FB', 'NFLX', 'MSFT') does not work
ax.set_xlabel('date')
ax.set_ylabel('Different Stocks')
ax.set_title('Stocks information')
ax.set_xticks([0 , 25, 50,75, 100])
plt.grid()
plt.show()
First, load the tips dataset
tips = sns.load_dataset('tips')
tips.head()
| total_bill | tip | sex | smoker | day | time | size | |
|---|---|---|---|---|---|---|---|
| 0 | 16.99 | 1.01 | Female | No | Sun | Dinner | 2 |
| 1 | 10.34 | 1.66 | Male | No | Sun | Dinner | 3 |
| 2 | 21.01 | 3.50 | Male | No | Sun | Dinner | 3 |
| 3 | 23.68 | 3.31 | Male | No | Sun | Dinner | 2 |
| 4 | 24.59 | 3.61 | Female | No | Sun | Dinner | 4 |
Let's explore this dataset. Pose a question and create a plot that support drawing answers for your question.
Some possible questions:
tips = sns.load_dataset('tips')
tips.head()
g= sns.FacetGrid(tips, col = 'sex', hue = 'smoker')
g.map(sns.scatterplot, 'total_bill', 'tip')
g.add_legend()
plt.savefig('smoker.png', dpi=200)
plt.show()
d= sns.FacetGrid(tips, col = 'sex', hue = 'day')
d.map(sns.scatterplot, 'total_bill', 'tip')
d.add_legend()
plt.savefig('smoker.png', dpi=200)
plt.show()
t= sns.FacetGrid(tips, col = 'sex', hue = 'time')
t.map(sns.scatterplot, 'total_bill', 'tip')
t.add_legend()
plt.savefig('smoker.png', dpi=200)
plt.show()
s= sns.FacetGrid(tips, col = 'sex', hue = 'size')
s.map(sns.scatterplot, 'total_bill', 'tip')
s.add_legend()
plt.savefig('smoker.png', dpi=200)
plt.show()
Redo the above exercises (challenges 2 & 3) with plotly express. Create diagrams which you can interact with.
Hints:
fig = px.line(stocks, x="date", y="GOOG", title= "Stock information")
fig.show()
fig = px.line(stocks, x="date", y=["GOOG", "AAPL", "AMZN", "FB", "NFLX", "MSFT"], title= "Stock information")
fig.show()
tips = sns.load_dataset('tips')
tips.head()
import plotly.express as px
df = px.data.tips()
fig = px.box(df, x="sex", y="total_bill", color="smoker")
fig.show()
fig = px.box(df, x="sex", y="total_bill", color="day")
fig.show()
fig = px.box(df, x="sex", y="total_bill", color="time")
fig.show()
fig = px.box(df, x="sex", y="total_bill", color="size")
fig.show()
Recreate the barplot below that shows the population of different continents for the year 2007.
Hints:
df = px.data.gapminder()
df.head()
#load data
df = px.data.gapminder()
df_2007 = df.query('year==2007')
df_2007_new = df_2007.groupby('continent').sum()
df_2007_new = df_2007_new.sort_values('pop', ascending=False)
fig = px.bar(df_2007_new, x="pop", y=df_2007_new.index, orientation='h', color = df_2007_new.index)
fig.show()